package com.yc.snack.controller;

import com.yc.snack.biz.ITypeInfoBiz;
import com.yc.snack.biz.impl.TypeInfoBizImpl;
import com.yc.snack.entity.TypeInfo;
import com.yc.snack.util.RequestParamUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/type/*")
public class TypeInfoController extends BaseController {

    /**
     * 添加类型
     *
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TypeInfo typeInfo = RequestParamUtil.getParams(TypeInfo.class, request);
        ITypeInfoBiz typeInfoBiz = new TypeInfoBizImpl();
        int result = typeInfoBiz.add(typeInfo);
        if (result > 0) {
            this.send(response, 200, "成功");
            return;
        }
        this.send(response, 500, "失败");
        return;
    }

    /**
     * 查询方法
     *
     * @param request
     * @param response
     */
    public void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 默认情况法，Layui分页发送的是 page（查第几页） 和 limit（每页显示多少条）
        int page = Integer.parseInt(request.getParameter("page"));
        int rows = Integer.parseInt(request.getParameter("limit"));

        ITypeInfoBiz typeInfoBiz = new TypeInfoBizImpl();
        Map<String, Object> map = typeInfoBiz.findByPage(page, rows);
        this.send(response, map);
    }

    /**
     * 查询所有
     * @param request
     * @param response
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ITypeInfoBiz typeInfoBiz = new TypeInfoBizImpl();
        List<TypeInfo> list =  typeInfoBiz.finds();
        if(list == null || list.isEmpty()) {
            this.send(response, 600, "暂无数据");
            return;
        }
        this.send(response, 200, list);
    }

}
